/**
* 左边菜单
*/ 
<template>
  <el-menu default-active="2" :collapse="collapsed" collapse-transition router :default-active="$route.path" unique-opened class="el-menu-vertical-demo" background-color="#334157" text-color="#fff" active-text-color="#ffd04b">
    <div class="logobox">
      <img class="logoimg" src="../assets/img/logo.png" alt="">
    </div>
    <el-submenu v-for="menu in menus" :key="menu.menuid" :index="menu.menuname">
      <template slot="title">
        <i class="iconfont" :class="menu.icon"></i>
        <span>{{menu.menuname}}</span>
      </template>
      <el-menu-item-group>
        <el-menu-item v-for="chmenu in menu.menus" :index="'/'+chmenu.url" :key="chmenu.menuid">
          <i class="iconfont" :class="chmenu.icon"></i>
          <span>{{chmenu.menuname}}</span>
        </el-menu-item>
      </el-menu-item-group>
    </el-submenu>
  </el-menu>
</template>
<script>
import { mapState } from 'vuex'
export default {
  name: 'leftnav',
  data() {
    return {
      collapsed: false,
      allmenu: [
        {
          menuid: 10,
          icon: 'li-icon-xiangmuguanli',
          menuname: '用户管理',
          hasThird: null,
          url: null,
          menus: [
            {
              menuid: 11,
              icon: 'li-icon-xiangmuguanli',
              menuname: '普通用户信息管理',
              hasThird: 'N',
              url: 'user',
              menus: null
            },
            {
              menuid: 12,
              icon: 'icon-cat-skuQuery',
              menuname: '订单信息管理',
              hasThird: 'N',
              url: 'order',
              menus: null
            },
            // {
            //   menuid: 14,
            //   icon: 'icon-cat-skuQuery',
            //   menuname: '评论信息管理',
            //   hasThird: 'N',
            //   url: 'comment',
            //   menus: null
            // }
          ]
        },
        {
          menuid: 20,
          icon: 'li-icon-dingdanguanli',
          menuname: '商品管理',
          hasThird: null,
          url: null,
          menus: [
            {
              menuid: 21,
              icon: 'icon-order-manage',
              menuname: '在售商品管理',
              hasThird: 'N',
              url: 'goods/sellGoods',
              menus: null
            },
            {
              menuid: 22,
              icon: 'icon-news-manage',
              menuname: '视窗推荐管理',
              hasThird: 'N',
              url: 'goods/recommend',
              menus: null
            }
          ]
        },
        {
          menuid: 30,
          icon: 'li-icon-xitongguanli',
          menuname: '品类管理',
          hasThird: null,
          url: null,
          menus: [
            {
              menuid: 32,
              icon: 'li-icon-zhifupeizhi1',
              menuname: '类别管理',
              hasThird: 'N',
              url: 'varity/varity',
              menus: null
            }
          ]
        },
        {
          menuid: 40,
          icon: 'li-icon-xitongguanli',
          menuname: '论坛管理',
          hasThird: null,
          url: null,
          menus: [
            {
              menuid: 41,
              icon: 'icon-cus-manage',
              menuname: '帖子管理',
              hasThird: 'N',
              url: 'daity/daity',
              menus: null
            },
            {
              menuid: 42,
              icon: 'li-icon-zhifupeizhi1',
              menuname: '评论管理',
              hasThird: 'N',
              url: 'daity/comment',
              menus: null
            }
          ]
        },
        {
          menuid: 50,
          icon: 'li-icon-shangchengxitongtubiaozitihuayuanwenjian91',
          menuname: '系统管理',
          hasThird: null,
          url: null,
          menus: [
            {
              menuid: 51,
              icon: 'icon-cat-skuQuery',
              menuname: '管理员信息管理',
              hasThird: 'N',
              url: 'userDetail',
              menus: null
            },
            {
              menuid: 52,
              icon: 'li-icon-jichuguanli',
              menuname: '销售数据报表',
              hasThird: 'N',
              url: 'charts/sell',
              menus: null
            }
          ]
        },
      ]
    }
  },
  computed: {
    ...mapState(['user']),
    menus () {
      let menus = this.filterMenu(this.allmenu, this.user.level)
      return menus
    }
  },
  // 创建完毕状态(里面是操作)
  created() {
    // 监听
    this.$root.Bus.$on('toggle', value => {
      this.collapsed = !value
    })
  },
  methods: {
    /**
     * 过滤路由表
     */
    filterMenu (allMenu, role) {
      const res = []
      allMenu.forEach(route => {
        if (this.hasPermission(route, role)) {
          // if (route.menus && route.menus.length > 0) {
          //   route.menus = this.filterMenu(route.menus, role)
          // }
          res.push(route)
        }
      })
      return res
    },
    /**
     * 判断该路由是否有权限
     */
    hasPermission (route, role) {
      if (route.roles && route.roles.length > 0) {
        return route.roles.some(item=> item === Number(role))
      } else {
        return true
      }
    }
  }
}
</script>
<style>
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 240px;
  min-height: 400px;
}
.el-menu-vertical-demo:not(.el-menu--collapse) {
  border: none;
  text-align: left;
}
.el-menu-item-group__title {
  padding: 0px;
}
.el-menu-bg {
  background-color: #1f2d3d !important;
}
.el-menu {
  border: none;
}
.logobox {
  height: 40px;
  line-height: 40px;
  color: #9d9d9d;
  font-size: 20px;
  text-align: center;
  padding: 20px 0px;
}
.logoimg {
  height: 40px;
}
</style>
